package com.intelligentpension.kotlinsimpleroom

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.intelligentpension.kotlinsimpleroom.dao.RoomBasic
import com.intelligentpension.kotlinsimpleroom.dao.User

open class MainAdapter(var context: Context, var dataList: MutableList<User>) :
    RecyclerView.Adapter<MainAdapter.ViewHolder>() {

    var dataBase: RoomBasic? = null

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var textView = itemView.findViewById<TextView>(R.id.text_view)
        var btEdit = itemView.findViewById<ImageView>(R.id.bt_edit)
        var btDelete = itemView.findViewById<ImageView>(R.id.bt_delete)
        var idText = itemView.findViewById<TextView>(R.id.text_id)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainAdapter.ViewHolder {
        var view =
            LayoutInflater.from(parent.context).inflate(R.layout.list_row_main, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: MainAdapter.ViewHolder, position: Int) {
        var data: User? = dataList?.get(position)

        dataBase = RoomBasic.getDatabase(context);

        holder.textView.text = data?.phone
        holder.idText.text=data?.id.toString()
        holder.btEdit.setOnClickListener {
            var d: User? = dataList?.get(holder.adapterPosition)

            var mId = d?.id
            var text: String? = d?.phone

            var dialog: Dialog = Dialog(context)
            dialog.setContentView(R.layout.dialog_up)
            var width = WindowManager.LayoutParams.MATCH_PARENT
            var height = WindowManager.LayoutParams.WRAP_CONTENT
            dialog.window!!.setLayout(width, height)
            dialog.show()

            var editText = dialog.findViewById<EditText>(R.id.edit_text)
            var btUpdate = dialog.findViewById<Button>(R.id.bt_update)
            editText.setText(text)
            btUpdate.setOnClickListener {
                dialog.dismiss()

                var updateText = editText.text.toString().trim();
                dataBase!!.UserDao().upData(mId!!, updateText)
                dataList!!.clear()
                dataList!!.addAll(dataBase!!.UserDao().selectAll()!!)
                notifyDataSetChanged()
            }

        }
        holder.btDelete.setOnClickListener {
            var d: User? = dataList?.get(holder.adapterPosition)
            dataBase!!.UserDao().delete(d!!)

            var pos = holder.adapterPosition
            var been = dataList?.get(position)
            dataList?.remove(been)
            notifyItemRemoved(pos)
            notifyItemRangeChanged(pos, dataList!!.size)
        }

    }

    override fun getItemCount(): Int = dataList!!.size


}
